1   /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 5.0 */
2   /* JavaCCOptions:KEEP_LINE_COL=null */
3   package org.apache.lucene.queryparser.flexible.standard.parser;
4    
5    import org.apache.lucene.queryparser.flexible.messages.Message;
6    import org.apache.lucene.queryparser.flexible.messages.MessageImpl;
7    import org.apache.lucene.queryparser.flexible.core.*;
8    import org.apache.lucene.queryparser.flexible.core.messages.*;
9   
10  /**
11   * This exception is thrown when parse errors are encountered.
12   * You can explicitly create objects of this exception type by
13   * calling the method generateParseException in the generated
14   * parser.
15   *
16   * You can modify this class to customize your error reporting
17   * mechanisms so long as you retain the public fields.
18   */
19  public class ParseException extends QueryNodeParseException {
20  
21    /**
22     * The version identifier for this Serializable class.
23     * Increment only if the <i>serialized</i> form of the
24     * class changes.
25     */
26    private static final long serialVersionUID = 1L;
27  
28    /**
29     * This constructor is used by the method "generateParseException"
30     * in the generated parser.  Calling this constructor generates
31     * a new object of this type with the fields "currentToken",
32     * "expectedTokenSequences", and "tokenImage" set.
33     */
34    public ParseException(Token currentTokenVal,
35       int[][] expectedTokenSequencesVal, String[] tokenImageVal) {
36       super(new MessageImpl(QueryParserMessages.INVALID_SYNTAX, initialise(
37       currentTokenVal, expectedTokenSequencesVal, tokenImageVal)));
38       this.currentToken = currentTokenVal;
39       this.expectedTokenSequences = expectedTokenSequencesVal;
40       this.tokenImage = tokenImageVal;
41     }
42  
43    /**
44     * The following constructors are for use by you for whatever
45     * purpose you can think of.  Constructing the exception in this
46     * manner makes the exception behave in the normal way - i.e., as
47     * documented in the class "Throwable".  The fields "errorToken",
48     * "expectedTokenSequences", and "tokenImage" do not contain
49     * relevant information.  The JavaCC generated code does not use
50     * these constructors.
51     */
52  
53    public ParseException() {
54       super(new MessageImpl(QueryParserMessages.INVALID_SYNTAX, "Error"));
55     }
56  
57    /** Constructor with message. */
58    public ParseException(Message message) {
59       super(message);
60     }
61  
62  
63    /**
64     * This is the last token that has been consumed successfully.  If
65     * this object has been created due to a parse error, the token
66     * followng this token will (therefore) be the first error token.
67     */
68    public Token currentToken;
69  
70    /**
71     * Each entry in this array is an array of integers.  Each array
72     * of integers represents a sequence of tokens (by their ordinal
73     * values) that is expected at this point of the parse.
74     */
75    public int[][] expectedTokenSequences;
76  
77    /**
78     * This is a reference to the "tokenImage" array of the generated
79     * parser within which the parse error occurred.  This array is
80     * defined in the generated ...Constants interface.
81     */
82    public String[] tokenImage;
83  
84    /**
85     * It uses "currentToken" and "expectedTokenSequences" to generate a parse
86     * error message and returns it.  If this object has been created
87     * due to a parse error, and you do not catch it (it gets thrown
88     * from the parser) the correct error message
89     * gets displayed.
90     */
91    private static String initialise(Token currentToken,
92                             int[][] expectedTokenSequences,
93                             String[] tokenImage) {
94      String eol = System.getProperty("line.separator", "\n");
95      StringBuilder expected = new StringBuilder();
96      int maxSize = 0;
97      for (int i = 0; i < expectedTokenSequences.length; i++) {
98        if (maxSize < expectedTokenSequences[i].length) {
99          maxSize = expectedTokenSequences[i].length;
100       }
101       for (int j = 0; j < expectedTokenSequences[i].length; j++) {
102         expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' ');
103       }
104       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
105         expected.append("...");
106       }
107       expected.append(eol).append("    ");
108     }
109     String retval = "Encountered \"";
110     Token tok = currentToken.next;
111     for (int i = 0; i < maxSize; i++) {
112       if (i != 0) retval += " ";
113       if (tok.kind == 0) {
114         retval += tokenImage[0];
115         break;
116       }
117       retval += " " + tokenImage[tok.kind];
118       retval += " \"";
119       retval += add_escapes(tok.image);
120       retval += " \"";
121       tok = tok.next;
122     }
123     retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
124     retval += "." + eol;
125     if (expectedTokenSequences.length == 1) {
126       retval += "Was expecting:" + eol + "    ";
127     } else {
128       retval += "Was expecting one of:" + eol + "    ";
129     }
130     retval += expected.toString();
131     return retval;
132   }
133 
134   /**
135    * The end of line string for this machine.
136    */
137   protected String eol = System.getProperty("line.separator", "\n");
138 
139   /**
140    * Used to convert raw characters to their escaped version
141    * when these raw version cannot be used as part of an ASCII
142    * string literal.
143    */
144   static String add_escapes(String str) {
145       StringBuilder retval = new StringBuilder();
146       char ch;
147       for (int i = 0; i < str.length(); i++) {
148         switch (str.charAt(i))
149         {
150            case 0 :
151               continue;
152            case '\b':
153               retval.append("\\b");
154               continue;
155            case '\t':
156               retval.append("\\t");
157               continue;
158            case '\n':
159               retval.append("\\n");
160               continue;
161            case '\f':
162               retval.append("\\f");
163               continue;
164            case '\r':
165               retval.append("\\r");
166               continue;
167            case '\"':
168               retval.append("\\\"");
169               continue;
170            case '\'':
171               retval.append("\\\'");
172               continue;
173            case '\\':
174               retval.append("\\\\");
175               continue;
176            default:
177               if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
178                  String s = "0000" + Integer.toString(ch, 16);
179                  retval.append("\\u" + s.substring(s.length() - 4, s.length()));
180               } else {
181                  retval.append(ch);
182               }
183               continue;
184         }
185       }
186       return retval.toString();
187    }
188 
189 }
190 /* JavaCC - OriginalChecksum=81401c29cf6f9909761c636b4778ccc0 (do not edit this line) */